/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller;

import cn.ac.nercis.pes.community.component.dialog.AlertDialog;
import cn.ac.nercis.pes.community.component.dialog.AlertMode;
import cn.ac.nercis.pes.community.component.dialog.AlertType;
import cn.ac.nercis.pes.community.controller.base.BaseWindow;
import cn.ac.nercis.pes.community.controller.dialog.UserInfoController;
import cn.ac.nercis.pes.community.support.FXMLController;
import cn.ac.nercis.pes.community.support.controls.JFXPopup;
import cn.ac.nercis.pes.community.support.theme.ThemeManager;
import cn.ac.nercis.pes.community.utils.*;
import cn.ac.nercis.pes.community.views.dialog.UserInfoView;
import cn.ac.nercis.pes.community.views.project.TaskManageView;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 主界面控制器
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
@FXMLController
public class MainController extends BaseWindow implements Initializable {

    @FXML
    private StackPane page;
    @FXML
    private BorderPane bodyPane;
    @FXML
    private ImageView appIcon;
    @FXML
    private HBox titleBar;
    @FXML
    private Button btnMin;
    @FXML
    private Button btnClose;
    @FXML
    private Button btnMax;
    @FXML
    private Button btnUser;

    @FXML
    private Button btnUpdate;

    @Autowired
    public MainController() {
    }

    /**
     * 初始化
     */
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.initControl();
        bodyPane.setCenter(SpringUtils.getBean(TaskManageView.class).getView());
        GuiStore.setMainWindowBody(this.bodyPane);
    }

    public void setCenter(Parent view){
        bodyPane.setCenter(view);
    }

    @FXML
    public void onMinWindow(){
        GuiStore.getStage().setIconified(true);
    }

    @FXML
    public void onMaxWindow(){
        this.windowMax(GuiStore.getStage());
        if(this.maximized){
            //最大化
            this.btnMax.setGraphic(IconUtils.getGlyph("win-res", "main-title-bar-res-icon"));
            this.btnMax.setTooltip(new Tooltip("还原"));
            this.page.getStyleClass().remove("page");
            this.page.getStyleClass().add("max-page");

        }else{
            //还原
            this.btnMax.setGraphic(IconUtils.getGlyph("win-max", "main-title-bar-max-icon"));
            this.btnMax.setTooltip(new Tooltip("最大化"));
            this.page.getStyleClass().remove("max-page");
            this.page.getStyleClass().add("page");
        }
    }

    @FXML
    public void onClose(){
        AlertDialog alertDialog = AlertDialog.builder()
                .withMode(AlertMode.CONFIRM)
                .withType(AlertType.WARN)
                .withTitle("操作提示")
                .withContent("确认退出应用吗？")
                .build();
        alertDialog.show((confirm)->{
            if(!confirm){
                return;
            }
            AppUtils.exitApp();
        });
    }

    public void initData(){
        this.loadUserInfo();
    }

    @FXML
    public void popUserInfo(){
        var userInfoView = SpringUtils.getBean(UserInfoView.class).getView();
        var popOver = new JFXPopup((Region) userInfoView);
        popOver.show(btnUser,JFXPopup.PopupVPosition.TOP,JFXPopup.PopupHPosition.LEFT,
                -50,btnUser.getHeight()+14);
        SpringUtils.getBean(UserInfoController.class).initData(popOver,this.page);
    }

    private void initControl(){
        this.appIcon.setImage(ThemeManager.config().getAppIcon());
        this.btnClose.setGraphic(IconUtils.getGlyph("win-close","main-title-bar-close-icon"));
        this.btnMin.setGraphic(IconUtils.getGlyph("win-min","main-title-bar-min-icon"));
        this.btnMax.setGraphic(IconUtils.getGlyph("win-max", "main-title-bar-max-icon"));
        this.btnUser.setGraphic(IconUtils.getGlyph("user-icon","main-window-btn-icon"));
        this.btnUpdate.setGraphic(IconUtils.getGlyph("update-icon","main-window-btn-icon"));
        this.titleBar.setOnMouseClicked((mouseEvent)->{
            if(mouseEvent.getButton().equals(MouseButton.PRIMARY) && mouseEvent.getClickCount() == DOUBLE_CLICK){
                onMaxWindow();
            }
        });
        ResizeWindowUtils.appendResizeFunc(GuiStore.getStage(),this.bodyPane);
        titleBar.setOnMouseDragged((event)->{
            if(!this.maximized && !GuiStore.getStage().isFullScreen()){
                this.onMouseDragged(event,GuiStore.getStage());
            }
        });
        titleBar.setOnMousePressed(this::onMousePressed);
        GuiStore.setRoot(this.page);
    }

    public void onIconClick(){
        setCenter(SpringUtils.getBean(TaskManageView.class).getView());
    }


    private void loadUserInfo(){
        this.btnUser.setText(AppStore.getCurrentUser().getDisplayName());
    }

}